<template>
	<view class="wrap">
		<u-navbar title="设置登录密码" :isBack="false"/>
		<view class="u-p-t-80">
			<text class="u-font-32">设置登录密码后可使用（手机号+密码）登录</text>
		</view>
		<view class="u-m-t-50">
			<input class="input" type="password" v-model="password" placeholder="请输入登录密码" placeholder-class="placeholder"/>
		</view>
		<view class="u-m-t-50">
			<input class="input" type="password" v-model="confirmPwd" placeholder="请输入确认密码" placeholder-class="placeholder"/>
		</view>
		<view class="next u-m-t-50" :class="{disable:!canNext}" @tap="next">
			下一步
		</view>
	</view>
</template>

<script>
	import { resetPassword } from '@/api/user';
	import { delay, goback, toast } from '@/utils/util';
	export default {
		data() {
			return {
				token:'',
				password:'',
				confirmPwd:'',
				lock:false,
				eventChannel:null
			};
		},
		computed:{
			canNext(){
				return !!this.password&&!!this.confirmPwd
			}
		},
		onLoad({token}) {
			this.token = token
			this.eventChannel = this.getOpenerEventChannel();
		},
		onBackPress({from}) {
			return from=='backbutton'
		},
		methods:{
			async next(){
				if(!this.canNext||this.lock){
					return
				}
				if(this.confirmPwd!=this.password){
					return toast('两次密码不一致')
				}
				try{
					this.lock = true
					await resetPassword({
						password:this.password
					},{token:this.token})
					uni.showToast({
						title:"设置成功"
					})
					
					await delay(1000)
					
					console.log(this.eventChannel);
					this.eventChannel.emit('successful',{password:this.password})
						uni.redirectTo({
							url:`/pages/improve/avatar?token=${this.token}`,
							success(){
								
							}
						})
					// goback()
				}finally{
					this.lock = false
				}
			}
		}
	}
</script>
<style>
	page{
		background-color: #FFFFFF;
	}
</style>
<style lang="scss" scoped>
	.wrap{
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.input{
		width: 600rpx;
		height: 100rpx;
		padding: 0 30rpx;
		border-radius: 20rpx;
		border: 4rpx solid #000000;
		font-size: 34rpx;
	}
	.next{
		font-size: 36rpx;
		font-weight: bold;
		width: 340rpx;
		height: 90rpx;
		border-radius: 200rpx;
		background-color: $theme-color;
		color: #000000;
		display: flex;
		align-items: center;
		justify-content: center;
		&.disable{
			background-color: #F0F0F0;
			color: #AAAAAA;
		}
	}
</style>
